package com.example.service.impl;

import com.example.entity.Lecture;
import com.example.entity.bo.CourseStudentResultBO;
import com.example.mapper.LectureMapper;
import com.example.service.LectureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class LectureServiceImpl implements LectureService {
    @Autowired(required = false)
    LectureMapper lectureMapper;

    @Override
    public List<Lecture> getByExampleWithLimit(String teacherName, String courseName, Date beginDate, Date endDate, Integer page, Integer limit) {
        int begin = (page - 1) * limit;
        return lectureMapper.selectByExampleWithLimit(teacherName, courseName, beginDate, endDate, begin, limit);
    }

    @Override
    public long getCountByExample(String teacherName, String courseName, Date beginDate, Date endDate) {
        return lectureMapper.countByExample2(teacherName, courseName, beginDate, endDate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add(Lecture lecture) {
        lectureMapper.insert(lecture);
    }

    @Override
    public Lecture getById(Integer id) {
        return lectureMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateById(Lecture lecture) {
        lectureMapper.updateByPrimaryKey(lecture);
    }

    @Override
    @Transactional
    public void removeById(Integer id) {
        lectureMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Lecture> getOrderByDateWithLimit(Date date, int n) {
        return lectureMapper.selectOrderByDateWithLimit(date,n);
    }

    @Override
    public List<CourseStudentResultBO> getCourseStudentResultsByExampleWithLimit(String teacherId, String courseId, String studentId, Integer page, Integer limit) {
        int begin = (page - 1) * limit;
        return lectureMapper.selectCourseStudentResultsByExampleWithLimit(teacherId,courseId,studentId,begin,limit);
    }
}
